import { 
	getProDeptTreeByLevelCode,
	getProDeptTreeByParentid,
	getProOrgTreeByParentid,
	getAllAuthForUser,
	getAllChildren,
	insertForStaff,
	updateForStaffBaseInfo,
	updateForStaffContactInfo,
	updateForStaffAccountInfo,
	updateForStaffEducationInfo,
	updateForStaffWorkInfo,
	updateForStaffTrainInfo,
	updateForStaffCertificateInfo,
	queryPageForProStaff,
	getAllStaffInfo,
	insertForStaffEducationInfo,
	insertForStaffWorkInfo,
	insertForStaffTrainInfo,
	insertForStaffCertificateInfo,
	deleteForStaffEducationInfo,
	deleteForStaffWorkInfo,
	deleteForStaffTrainInfo,
	deleteForStaffCertificateInfo,
	deleteByPrimaryKeyForProStaff,
	upLoadListPersonInformation,
	getAllPostForUser
} from '@/services/business/api_staff';

export default {
    namespace: 'prostaff',

    state: {
        data: {
            list: [],
            pagination: {},
        },
        authForUser: {
        	list:[],
        },
        selectDeptTree: {
        	list:[],
        },
        cascaderList: {
        	list: [],
        },
        staffInfo: {
        	map: {
        		person_information:null,
				person_communication_information:null,
				person_account_information: null,
				person_educational_experience: null,
				person_work_experience: null,
				person_training: null,
				person_certificate: null
        	},
        },
        postForUser: {
        	list:[],
        },
    },

    effects: {
            *get_prodepttree_by_levelcode({ payload, callback }, { call, put }) {
                const response = yield call(getProDeptTreeByLevelCode, payload);
                yield put({
                    type: 'getProDeptTreeByLevelCode',
                    payload: response,
                });

                if (callback) callback();
            },
            *get_prodepttree_by_parentid({ payload, callback }, { call, put }) {
                const response = yield call(getProDeptTreeByParentid, payload);
                yield put({
                    type: 'getProDeptTreeByParentid',
                    payload: response,
                });

                if (callback) callback(response);
            },
            *get_all_auth_for_user({ payload, callback }, { call, put }) {
                const response = yield call(getAllAuthForUser, payload);
                yield put({
                    type: 'getAllAuthForUser',
                    payload: response,
                });

                if (callback) callback(response);
            },
            *get_all_city({ payload, callback }, { call, put }) {
                const response = yield call(getAllChildren, payload);
                yield put({
                    type: 'getAllCity',
                    payload: response,
                });

                if (callback) callback(response);
            },
            *insert_for_staff({ payload, callback }, { call, put }) {
                const response = yield call(insertForStaff, payload);

                if (callback) callback(response);
            },
            *upload_list_personinformation({ payload, callback }, { call, put }) {
                const response = yield call(upLoadListPersonInformation, payload);
								const responseData = yield call(queryPageForProStaff, {});
                yield put({
                    type: 'queryPageForProStaff',
                    payload: responseData,
                });
                if (callback) callback(response);
            },
            *update_for_staff_baseinfo({ payload, callback }, { call, put }) {
                const response = yield call(updateForStaffBaseInfo, payload);
                const newPayload = {punid: payload.id};
								const responseData = yield call(getAllStaffInfo, newPayload);
                yield put({
                    type: 'getAllStaffInfo',
                    payload: responseData,
                });
                if (callback) callback(response);
            },
            *update_for_staff_contactinfo({ payload, callback }, { call, put }) {
                const response = yield call(updateForStaffContactInfo, payload);
								const newPayload = {punid: payload.id};
								const responseData = yield call(getAllStaffInfo, newPayload);
                yield put({
                    type: 'getAllStaffInfo',
                    payload: responseData,
                });
                if (callback) callback(response);
            },
            *update_for_staff_accountinfo({ payload, callback }, { call, put }) {
                const response = yield call(updateForStaffAccountInfo, payload);
								const newPayload = {punid: payload.id};
								const responseData = yield call(getAllStaffInfo, newPayload);
                yield put({
                    type: 'getAllStaffInfo',
                    payload: responseData,
                });
                if (callback) callback(response);
            },
            *insert_for_staff_educationinfo({ payload, callback }, { call, put }) {
                const response = yield call(insertForStaffEducationInfo, payload);
								const newPayload = {punid: payload.punid};
								const responseData = yield call(getAllStaffInfo, newPayload);
                yield put({
                    type: 'getAllStaffInfo',
                    payload: responseData,
                });
                if (callback) callback(response);
            },
            *update_for_staff_educationinfo({ payload, callback }, { call, put }) {
                const response = yield call(updateForStaffEducationInfo, payload);
								const newPayload = {punid: payload.punid};
								const responseData = yield call(getAllStaffInfo, newPayload);
                yield put({
                    type: 'getAllStaffInfo',
                    payload: responseData,
                });
                if (callback) callback(response);
            },
            *delete_for_staff_educationinfo({ payload, callback }, { call, put }) {
                const response = yield call(deleteForStaffEducationInfo, payload);
								const newPayload = {punid: payload.punid};
								const responseData = yield call(getAllStaffInfo, newPayload);
                yield put({
                    type: 'getAllStaffInfo',
                    payload: responseData,
                });
                if (callback) callback(response);
            },
            *insert_for_staff_workinfo({ payload, callback }, { call, put }) {
                const response = yield call(insertForStaffWorkInfo, payload);
								const newPayload = {punid: payload.punid};
								const responseData = yield call(getAllStaffInfo, newPayload);
                yield put({
                    type: 'getAllStaffInfo',
                    payload: responseData,
                });
                if (callback) callback(response);
            },
            *update_for_staff_workinfo({ payload, callback }, { call, put }) {
                const response = yield call(updateForStaffWorkInfo, payload);
								const newPayload = {punid: payload.punid};
								const responseData = yield call(getAllStaffInfo, newPayload);
                yield put({
                    type: 'getAllStaffInfo',
                    payload: responseData,
                });
                if (callback) callback(response);
            },
            *delete_for_staff_workinfo({ payload, callback }, { call, put }) {
                const response = yield call(deleteForStaffWorkInfo, payload);
								const newPayload = {punid: payload.punid};
								const responseData = yield call(getAllStaffInfo, newPayload);
                yield put({
                    type: 'getAllStaffInfo',
                    payload: responseData,
                });
                if (callback) callback(response);
            },
            *insert_for_staff_traininfo({ payload, callback }, { call, put }) {
                const response = yield call(insertForStaffTrainInfo, payload);
								const newPayload = {punid: payload.punid};
								const responseData = yield call(getAllStaffInfo, newPayload);
                yield put({
                    type: 'getAllStaffInfo',
                    payload: responseData,
                });
                if (callback) callback(response);
            },
            *update_for_staff_traininfo({ payload, callback }, { call, put }) {
                const response = yield call(updateForStaffTrainInfo, payload);
								const newPayload = {punid: payload.punid};
								const responseData = yield call(getAllStaffInfo, newPayload);
                yield put({
                    type: 'getAllStaffInfo',
                    payload: responseData,
                });
                if (callback) callback(response);
            },
            *delete_for_staff_traininfo({ payload, callback }, { call, put }) {
                const response = yield call(deleteForStaffTrainInfo, payload);
								const newPayload = {punid: payload.punid};
								const responseData = yield call(getAllStaffInfo, newPayload);
                yield put({
                    type: 'getAllStaffInfo',
                    payload: responseData,
                });
                if (callback) callback(response);
            },
            *insert_for_staff_certificateinfo({ payload, callback }, { call, put }) {
                const response = yield call(insertForStaffCertificateInfo, payload);
								const newPayload = {punid: payload.punid};
								const responseData = yield call(getAllStaffInfo, newPayload);
                yield put({
                    type: 'getAllStaffInfo',
                    payload: responseData,
                });
                if (callback) callback(response);
            },
            *update_for_staff_certificateinfo({ payload, callback }, { call, put }) {
                const response = yield call(updateForStaffCertificateInfo, payload);
								const newPayload = {punid: payload.punid};
								const responseData = yield call(getAllStaffInfo, newPayload);
                yield put({
                    type: 'getAllStaffInfo',
                    payload: responseData,
                });
                if (callback) callback(response);
            },
            *delete_for_staff_certificateinfo({ payload, callback }, { call, put }) {
                const response = yield call(deleteForStaffCertificateInfo, payload);
								const newPayload = {punid: payload.punid};
								const responseData = yield call(getAllStaffInfo, newPayload);
                yield put({
                    type: 'getAllStaffInfo',
                    payload: responseData,
                });
                if (callback) callback(response);
            },
            *query_page_for_prostaff({ payload, callback }, { call, put }) {
                const response = yield call(queryPageForProStaff, payload);
                yield put({
                    type: 'queryPageForProStaff',
                    payload: response,
                });

                if (callback) callback();
            },
            *get_all_staff_info({ payload, callback }, { call, put }) {
                const response = yield call(getAllStaffInfo, payload);
                yield put({
                    type: 'getAllStaffInfo',
                    payload: response,
                });

                if (callback) callback();
            },
            *delete_by_primarykey_for_prostaff({ payload, callback }, { call, put }) {
                const response = yield call(deleteByPrimaryKeyForProStaff, payload);
								
								const newPayload = {currentPage: payload.currentPage,pageSize: payload.pageSize};
                const responsedata = yield call(queryPageForProStaff, newPayload);
                yield put({
	                type: 'queryPageForProStaff',
	                payload: responsedata,
                });
				if (callback) callback(response);
            },
            *get_all_post_for_user({ payload, callback }, { call, put }) {
                const response = yield call(getAllPostForUser, payload);
                yield put({
                    type: 'getAllPostForUser',
                    payload: response,
                });

                if (callback) callback(response);
            },
    },

    reducers: {
        getProDeptTreeByLevelCode(state, action) {
            return {
                ...state,
                selectDeptTree: action.payload,
            };
        },
        getProDeptTreeByParentid(state, action) {
            return {
                ...state,
                selectDeptTree: action.payload,
            };
        },
        getAllAuthForUser(state, action) {
            return {
                ...state,
                authForUser: action.payload,
            };
        },
        getAllCity(state, action) {
            return {
                ...state,
                cascaderList: action.payload,
            };
        },
        queryPageForProStaff(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
        getAllStaffInfo(state, action) {
            return {
                ...state,
                staffInfo: action.payload,
            };
        },
        getAllPostForUser(state, action) {
            return {
                ...state,
                postForUser: action.payload,
            };
        },
    },
};
